package cn.sieis.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sieis.pojo.Contest;
import cn.sieis.pojo.ContestVo;
import cn.sieis.pojo.Level;
import cn.sieis.service.contest.ContestService;
import cn.sieis.service.level.LevelService;

@Controller
@RequestMapping("/front/contest")
public class ContestFrontController {

	@Resource
	private ContestService contestService;

	@Resource
	private LevelService levelService;

	@RequestMapping(value = "/list.html")
	public String getContestListFr(Model model, String conName, Integer conLevel) {
		List<Level> levelList = null;
		List<Contest> contestList = null;
		try {
			contestList = contestService.getContestListFr(conName, conLevel);
			levelList = levelService.getLevelList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("contestList", contestList);
		model.addAttribute("levelList", levelList);
		return "front/contestlist";
	}

	@RequestMapping(value = "contestlist.html")
	public String index(Model model) {
		return "front/contestlist";
	}

	@RequestMapping(value = "contestview.html", method = RequestMethod.GET)
	public String contestview(@RequestParam("id") String id, Model model, HttpServletRequest request) {
		try {
			Contest contest = contestService.getContestById(Integer.valueOf(id));
			ContestVo contestVo = new ContestVo();
			contestVo.setAppName(contest.getAppName());
			contestVo.setConName(contest.getConName());
			Integer conApp = contest.getConApp();
			if (conApp != null) {
				switch (conApp) {
				case 1:
					contestVo.setConApp("申请未审核");
					break;
				case 2:
					contestVo.setConApp("申请不通过");
					break;
				case 3:
					contestVo.setConApp("申请通过");
					break;

				}

			}
			contestVo.setConCode(contest.getConCode());
			Integer level = contest.getConLevel();
			if (level != null) {
				switch (level) {
				case 1:
					contestVo.setConLevel("001");
					break;
				case 2:
					contestVo.setConLevel("002");
					break;
				case 3:
					contestVo.setConLevel("003");
					break;
				case 4:
					contestVo.setConLevel("004");
					break;
				}
			}
			contestVo.setConName(contest.getConName());
			contestVo.setDetails(contest.getDetails());
			contestVo.setId(contest.getId());//conName
			contestVo.setLevName(contest.getLevName());
			model.addAttribute("contest", contestVo);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "front/contestview";
	}

}
